//+---------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation, 2007
//
// File: DataProviderHelpers.cs
//
// Contents: Helpers for DeliveryService, for things like dealing with IDataProvider
//
// Created by: Adam Wardynski (adamwa) Feb 18 2008
//
//----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.MSAS.ManagedAPI;

namespace Microsoft.Reputation.Services
{
    internal class DeliveryServiceHelpers
    {
        internal static bool IsFatalException(Exception exception)
        {
            return (exception is OutOfMemoryException ||
                 exception is StackOverflowException ||
                 exception is ExecutionEngineException);
        }
    }

    internal class LookupState
    {
        //
        // Request part stored for logging
        //
        public readonly ClientIdToken ClientId;
        public readonly Filter Filter;
        public readonly StateToken RequestStateToken;
        public readonly string ClientIpAddress;

        //
        // Fields for creating the response
        //
        public LookupRequest[] Requests;
        public StateToken ResponseStateToken;
        public uint Status;
        public long LatestOverridesStamp;
        public long DataProviderLatency;

        public LookupState(
            ClientIdToken clientId,
            LookupRequest[] requests,
            Filter filter,
            StateToken requestStateToken,
            string clientIpAddress)
        {
            this.ClientId = clientId;
            this.Requests = requests;
            this.Filter = filter;
            this.ClientIpAddress = clientIpAddress;

            if(requestStateToken != null)
            {
                this.RequestStateToken = requestStateToken;
                this.ResponseStateToken = new StateToken(requestStateToken);
            }
            
            this.Status = StatusCodes.OK;
            this.LatestOverridesStamp = MrsDeliveryService.InvalidOverridesStamp;
        }
    }

    internal class LookupAsyncResult
      : AsyncResultOwnState<List<LookupResponse>, LookupState>
    {
        internal LookupAsyncResult(LookupState ownState, AsyncCallback hostCallback, object hostState)
            : base(ownState, hostCallback, hostState)
        {
        }
    }

    internal class ListingAsyncResult
      : AsyncResultOwnState<List<ListingResponse>, ListingState>
    {
        internal ListingAsyncResult(
            ListingState ownState,
            AsyncCallback hostCallback,
            object hostState)
            : base(ownState, hostCallback, hostState)
        {
        }
    }

    internal class ListingState
    {                
        //
        // Request part stored for logging
        //
        public readonly ClientIdToken ClientId;
        public readonly ListingRequest[] Requests;
        public readonly Filter Filter;
        public readonly StateToken RequestStateToken;
        public readonly ListingToken RequestListingToken;
        //
        // Fields for the response
        //
        public StateToken ResponseStateToken;
        public ListingToken ResponseListingToken;
        public long LatestOverridesStamp;
        public uint Remaining;
        public uint Status;
        public long DataProviderLatency;

        internal ListingState(
            ClientIdToken clientId,
            ListingRequest[] requests,
            Filter filter,
            StateToken requestStateToken,
            ListingToken requestListingToken)
        {
            this.ClientId = clientId;
            this.Requests = requests;
            this.Filter = filter;

            if (requestStateToken != null)
            {
                this.RequestStateToken = requestStateToken;
                this.ResponseStateToken = new StateToken(requestStateToken);
            }
            if (requestListingToken != null)
            {
                this.RequestListingToken = requestListingToken;
                this.ResponseListingToken = new ListingToken(requestListingToken);
            }
            this.Status = StatusCodes.OK;
            this.LatestOverridesStamp = MrsDeliveryService.InvalidOverridesStamp;
            this.Remaining = 0;
        }
    }

    internal class LookupUrlState
    {
        //
        // Request part stored for logging
        //
        public readonly ClientIdToken ClientId;
        public readonly string ClientIpAddress;
        public readonly string Url;

        //
        // Response Parts
        //
        public uint Status;
        public long DataProviderLatency;

        /// <summary>
        /// Utilized in both the request and the response to 
        /// do the creation of the datablock and to categorize
        /// the results.
        /// </summary>
        public ClientCategorizeProcessor Categorizer;

        public LookupUrlState(ClientIdToken clientId, string clientIpAddress, string url)
        {
            this.ClientId = clientId;
            this.ClientIpAddress = clientIpAddress;
            this.Url = url;
        }
    }

    internal class LookupUrlAsyncResult
        : AsyncResultOwnState<List<CategoryReputation>, LookupUrlState>
    {
        internal LookupUrlAsyncResult(LookupUrlState ownState, AsyncCallback hostCallback, object hostState)
            : base(ownState, hostCallback, hostState)
        {
        }
    }
}
